<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <view class="jtop_c">切换身份</view>
    </view>
    <view class="tophr"></view>

    <!-- <view class="ust" v-if="user.corp_id">
			<text class="ust1">中南建筑</text><image src="../../static/img/user/ic_certificate.png" class="ust2"></image>
			<image src="../../static/img/user/ic_check.png" class="ust3"></image>
		</view> -->

    <view class="usts" v-if="items.length > 0">
      <view
        class="ust"
        :class="true == item.defaultCheck ? 'selectBox' : ''"
        @click="labelBtn(index)"
        v-for="(item, index) in items"
        :key="item.id"
      >
        <text class="ust1 ustss ell">{{ item.fullName }}</text
        ><image
          src="../../static/img/user/ic_certificate.png"
          class="ust2 ustss ell"
        ></image>
        <image src="../../static/img/user/ic_check.png" class="ust3"></image>
      </view>
    </view>

    <view class="ua_as">
      <view class="ua_a" @tap="to_add">
        <image src="../../static/img/user/ic_company.png" class="ua_a1"></image>
        <view class="ua_al">
          <text class="ua_a2">创建企业/团队</text>
          <image src="../../static/img/user/ic_right.png" class="ua_a3"></image>
        </view>
      </view>
      <!-- <view class="ua_a" @tap="to_reg">
				<image src="../../static/img/user/ic_plus_circle.png" class="ua_a1"></image>
				<view class="ua_al">
					<text  class="ua_a2">添加我的其他企业身份</text>
					<image src="../../static/img/user/ic_right.png" class="ua_a3"></image>
				</view>
			</view> -->
    </view>

    <text class="ua_t3">© 2020 modou.com All Rights Reserved.</text>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  data() {
    return {
      user: {},
      items: [],
      // 当前项
      jqedit: '',
    };
  },
  onShow() {
    // 获取列表信息
    request({
      url: '/ucenter/corp-user/corp-list',
      method: 'GET',

      data: {
        page: 1,
        size: 10000,
      },
      success: res => {
        console.log('获取列表信息:', res);
        if (res.data.success == true) {
          this.items = res.data.data;
          console.log('获取列表信息:');
          //设置当前项
          for (let i in this.items) {
            if (this.items[i].defaultCheck) {
              this.jqedit = i;
              console.log(this.jqedit);
              return;
            }
          }
        } else {
          this.repeat_click = true;
          uni.showToast({
            title: res.data.msg,
            icon: 'none',
          });
        }
      },
      fail: () => {
        this.repeat_click = true;
        uni.showToast({
          title: '服务器开小差，请稍后~',
          icon: 'none',
        });
        return false;
      },
    });
  },
  methods: {
    labelBtn(index) {
      //判断是否点前项目
      if (index == this.jqedit) {
        return;
      }
      this.jqedit = index;

      for (let i in this.items) {
        this.items[i].defaultCheck = false;
      }

      this.items[index].defaultCheck = true;

      var jtitle = this.items[index];

      // var aa = uni.getStorageSync("user");
      // aa["corp_name"] = this.items[index].corp.fullName;
      // aa["corp_id"] = this.items[index].corp.id;
      // uni.setStorageSync("user",aa);

      request({
        url: '/ucenter/user/set-primary-corp/' + this.items[index].id,
        // data: {
        // 	phone: phone
        // },

        method: 'PUT',
        success: openIdRes => {
          console.log(openIdRes);

          // 已经授权了、查询到用户的数据了
          if (openIdRes.data.success == true) {
            this.repeat_click = true;
            uni.showToast({
              title: '切换成功',
            });

            uni.removeStorageSync('dqfull_name');
          } else {
            // 错误
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    ...mapMutations(['logout']),
    bindLogin() {
      uni.navigateTo({
        url: '../login/login',
      });
    },

    to_reg() {
      uni.removeStorageSync('user_id');
      uni.removeStorageSync('refresh_token');
      uni.removeStorageSync('expires_in');
      uni.removeStorageSync('access_token');
      uni.removeStorageSync('token_type');

      uni.removeStorageSync('user');

      uni.navigateTo({
        url: '../reg/reg',
      });
    },

    to_add() {
      uni.navigateTo({
        url: '../user/user_select_add',
      });
    },

    bindLogout() {
      this.logout();
      /**
       * 如果需要强制登录跳转回登录页面
       */
      if (this.forcedLogin) {
        uni.reLaunch({
          url: '../login/login',
        });
      }
    },
    navigateBack() {
      uni.navigateTo({
        url: '../user/user_set',
      });
      // uni.navigateBack()
    },
  },
};
</script>

<style>
.usts {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-bottom: 0;
  border-right: 0;
  background: #ffffff;
}
.ust {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 0rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-top: 0;
  border-right: 0;
  background: #ffffff;
}
.ust1 {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 34rpx;
  line-height: 45rpx;
  color: #000000;
  margin: 25rpx 0 30rpx 30rpx;
}
.ust2 {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: left;
  margin: 30rpx 0 0 10rpx;
}
.ust3 {
  width: 48rpx;
  height: 48rpx;
  display: none;
  float: right;
  margin: 21rpx 30rpx 0 0;
}
.selectBox .ust3 {
  display: block;
}

.ustss {
  max-width: 550rpx;
  display: inline-block;
}

.ua_t {
  width: 180rpx;
  height: 180rpx;
  display: block;
  float: right;
  margin: 110rpx 285rpx 0;
  border-radius: 24rpx;
}
.ua_t1 {
  width: 100%;
  height: auto;
  display: block;
  float: right;
  font-size: 36rpx;
  line-height: 44rpx;
  text-align: center;
  color: #000000;
  margin: 20rpx 0rpx 0 0;
}
.ua_t2 {
  width: 100%;
  height: auto;
  display: block;
  float: right;
  font-size: 24rpx;
  line-height: 32rpx;
  text-align: center;
  color: #999999;
  margin: 10rpx 0rpx 0 0;
}
.ua_t3 {
  width: 100%;
  height: auto;
  display: block;
  position: fixed;
  left: 0;
  bottom: 50rpx;
  float: right;
  font-size: 24rpx;
  line-height: 32rpx;
  text-align: center;
  color: #999999;
  margin: 300rpx 0rpx 0 0;
}

.ua_as {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}
.ua_a {
  width: 720rpx;
  height: 90rpx;
  display: block;
  float: right;
  line-height: 90rpx;
}
.ua_al {
  width: 640rpx;
  height: 90rpx;
  display: block;
  float: right;
  line-height: 90rpx;
  border-bottom: 1px solid #dddddd;
}
.ua_as .ua_a:last-child {
  border: 0;
}
.ua_a1 {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 22rpx 32rpx 0 0;
}
.ua_a2 {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 34rpx;
  line-height: 45rpx;
  color: #256aff;
  margin: 25rpx 0 0 0;
}
.ua_a3 {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}
.ua_a4 {
  width: auto;
  height: auto;
  display: block;
  float: right;
  font-size: 32rpx;
  line-height: 45rpx;
  color: #999999;
  margin: 26rpx 30rpx 0 0;
}

.ub_t {
  width: auto;
  height: 40rpx;
  display: block;
  float: right;
  font-size: 32rpx;
  line-height: 40rpx;
  text-align: right;
  color: #999999;
  margin: 25rpx 20rpx 0 0;
}
.ub_t image {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: right;
  margin: 0rpx 0rpx 0 6rpx;
}
.ub_tx {
  width: 114rpx;
  height: 114rpx;
  display: block;
  float: right;
  margin: 30rpx 20rpx 30rpx 0;
  border-radius: 8rpx;
}
.uba {
  height: auto;
}
.uba .ub_l {
  margin-top: 45rpx;
}
.uba .ub_jt {
  margin-top: 75rpx;
}
.ubscc {
  text-align: center;
  line-height: 90rpx;
  color: #eb5342;
  font-size: 32rpx;
}
.js {
  width: 720rpx;
  height: auto;
  display: block;
  float: right;
  margin: 25rpx 0 10rpx;
  font-size: 28rpx;
  line-height: 36rpx;
  color: #888888;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}
</style>
